<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户管理 - Sentinel LDK Licensing</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
            background: white;
            border-radius: 15px;
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            overflow: hidden;
        }
        
        .header {
            background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
            color: white;
            padding: 30px;
            text-align: center;
        }
        
        .header h1 {
            font-size: 2.5em;
            margin-bottom: 10px;
        }
        
        .header p {
            font-size: 1.1em;
            opacity: 0.9;
        }
        
        .content {
            padding: 40px;
        }
        
        .form-section {
            background: #f8f9fa;
            padding: 30px;
            border-radius: 10px;
            margin-bottom: 30px;
        }
        
        .form-section h2 {
            color: #333;
            margin-bottom: 20px;
            font-size: 1.5em;
        }
        
        .form-group {
            margin-bottom: 20px;
        }
        
        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: #555;
            font-weight: 500;
        }
        
        .form-group input {
            width: 100%;
            padding: 12px 15px;
            border: 2px solid #e1e5e9;
            border-radius: 8px;
            font-size: 16px;
            transition: border-color 0.3s ease;
        }
        
        .form-group input:focus {
            outline: none;
            border-color: #4facfe;
            box-shadow: 0 0 0 3px rgba(79, 172, 254, 0.1);
        }
        
        .btn {
            background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
            color: white;
            padding: 12px 30px;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            cursor: pointer;
            transition: transform 0.2s ease, box-shadow 0.2s ease;
        }
        
        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 20px rgba(79, 172, 254, 0.3);
        }
        
        .btn-danger {
            background: linear-gradient(135deg, #ff6b6b 0%, #ee5a52 100%);
        }
        
        .btn-danger:hover {
            box-shadow: 0 10px 20px rgba(255, 107, 107, 0.3);
        }
        
        .users-section {
            margin-top: 30px;
        }
        
        .users-section h2 {
            color: #333;
            margin-bottom: 20px;
            font-size: 1.5em;
        }
        
        .user-list {
            display: grid;
            gap: 20px;
        }
        
        .user-card {
            background: white;
            border: 2px solid #e1e5e9;
            border-radius: 10px;
            padding: 20px;
            transition: transform 0.2s ease, box-shadow 0.2s ease;
        }
        
        .user-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 20px rgba(0,0,0,0.1);
        }
        
        .user-info {
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .user-details h3 {
            color: #333;
            margin-bottom: 5px;
        }
        
        .user-details p {
            color: #666;
            margin-bottom: 3px;
        }
        
        .user-actions {
            display: flex;
            gap: 10px;
        }
        
        .message {
            padding: 15px;
            border-radius: 8px;
            margin-bottom: 20px;
            display: none;
        }
        
        .message.success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        
        .message.error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        
        @media (max-width: 768px) {
            .content {
                padding: 20px;
            }
            
            .user-info {
                flex-direction: column;
                align-items: flex-start;
                gap: 15px;
            }
            
            .user-actions {
                width: 100%;
                justify-content: flex-end;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>用户管理系统</h1>
            <p>Sentinel LDK Licensing - 用户管理演示</p>
        </div>
        
        <div class="content">
            <div id="message" class="message"></div>
            
            <div class="form-section">
                <h2>添加新用户</h2>
                <form id="userForm">
                    <div class="form-group">
                        <label for="username">用户名:</label>
                        <input type="text" id="username" name="username" required placeholder="请输入用户名">
                    </div>
                    <div class="form-group">
                        <label for="email">邮箱:</label>
                        <input type="email" id="email" name="email" required placeholder="请输入邮箱地址">
                    </div>
                    <button type="submit" class="btn">添加用户</button>
                </form>
            </div>
            
            <div class="users-section">
                <h2>用户列表</h2>
                <div id="userList" class="user-list">
                    <!-- 用户列表将在这里动态加载 -->
                </div>
            </div>
        </div>
    </div>
    
    <script>
        // API 基础URL
        const API_BASE = '/api/license';
        
        // 显示消息
        function showMessage(text, type = 'success') {
            const messageEl = document.getElementById('message');
            messageEl.textContent = text;
            messageEl.className = `message ${type}`;
            messageEl.style.display = 'block';
            
            setTimeout(() => {
                messageEl.style.display = 'none';
            }, 3000);
        }
        
        // 加载用户列表
        async function loadUsers() {
            try {
                const response = await fetch(`${API_BASE}/users`);
                const users = await response.json();
                
                const userListEl = document.getElementById('userList');
                
                if (users.length === 0) {
                    userListEl.innerHTML = '<p style="text-align: center; color: #666; padding: 40px;">暂无用户数据</p>';
                    return;
                }
                
                userListEl.innerHTML = users.map(user => `
                    <div class="user-card">
                        <div class="user-info">
                            <div class="user-details">
                                <h3>${user.username}</h3>
                                <p><strong>邮箱:</strong> ${user.email}</p>
                                <p><strong>ID:</strong> ${user.id}</p>
                                <p><strong>创建时间:</strong> ${new Date(user.createdAt).toLocaleString('zh-CN')}</p>
                            </div>
                            <div class="user-actions">
                                <button class="btn btn-danger" onclick="deleteUser(${user.id})">删除</button>
                            </div>
                        </div>
                    </div>
                `).join('');
            } catch (error) {
                console.error('加载用户列表失败:', error);
                showMessage('加载用户列表失败', 'error');
            }
        }
        
        // 添加用户
        async function addUser(userData) {
            try {
                const response = await fetch(`${API_BASE}/users`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(userData)
                });
                
                if (response.ok) {
                    const user = await response.json();
                    showMessage(`用户 "${user.username}" 添加成功!`);
                    loadUsers(); // 重新加载用户列表
                    document.getElementById('userForm').reset(); // 清空表单
                } else {
                    throw new Error('添加用户失败');
                }
            } catch (error) {
                console.error('添加用户失败:', error);
                showMessage('添加用户失败', 'error');
            }
        }
        
        // 删除用户
        async function deleteUser(userId) {
            if (!confirm('确定要删除这个用户吗？')) {
                return;
            }
            
            try {
                const response = await fetch(`${API_BASE}/users/${userId}`, {
                    method: 'DELETE'
                });
                
                if (response.ok) {
                    const result = await response.text();
                    showMessage(result);
                    loadUsers(); // 重新加载用户列表
                } else {
                    throw new Error('删除用户失败');
                }
            } catch (error) {
                console.error('删除用户失败:', error);
                showMessage('删除用户失败', 'error');
            }
        }
        
        // 表单提交事件
        document.getElementById('userForm').addEventListener('submit', function(e) {
            e.preventDefault();
            
            const formData = new FormData(this);
            const userData = {
                username: formData.get('username'),
                email: formData.get('email')
            };
            
            addUser(userData);
        });
        
        // 页面加载完成后加载用户列表
        document.addEventListener('DOMContentLoaded', function() {
            loadUsers();
        });
    </script>
</body>
</html>